<?php
/**
 * 创建模型类
 * 
 * 类名
 * ml_model_db...后面接自己的名字，驼峰写法
 *
 * 
 */
class ml_model_dbAtMe extends Lib_datamodel_db 
{
    
    /**
     * 创建构造函数
     *
     */
    function __construct()
    {
        $db_config = ml_factory::load_standard_conf('db');
        parent::__construct('atme' , $db_config['atme']);
    }
    
    protected function hook_after_fetch()
    {
        $data = $this->get_data();

        if(isset($data['atme_info']))
        {
            $data['atme_info'] = unserialize($data['atme_info']);
            $this->set_data($data);
        }else
        {
            foreach($data as $key=>$val)
            {
                if(isset($val['atme_info']))
                {
                    $val['atme_info'] =unserialize($val['atme_info']);
                }
                $arr[$key]=$val;
            }
            $this->set_data($arr);
        }

        return true;
    }
    protected function hook_before_write($array)
    {
        if(isset($array['atme_info']))
        {
            $array['atme_info'] = serialize($array['atme_info']);
        }
        return $array;
    }

    
    public function add_atme($uid , $type , $data , $ctime , &$atme_rid)
    {
        if(!$this->init_db($uid, self::DB_MASTER))
            return false;
        
        $data['uid'] = $uid;
        $data['type'] = $type;
        $data['ctime'] = $ctime;
        $rs = $this->insert($data);
        if(!$rs)
            return false;
        else 
        {
            $atme_rid = ml_tool_resid::make_resid($uid , ML_RESID_TYPE_ATME , $this->insert_id());
            return false;
        }
    }
    
    public function getListByUid($uid , $page = 1 , $pagesize = 40)
    {
        if(!$this->init_db($uid, self::DB_SLAVE ))
            return false;
            
        $start = ($page) * $pagesize;
        $sql = 'select * from `'.$this->table.'` where `uid`='.$uid.' and status=1 order by `ctime` desc limit '.$start.' , '.$pagesize;
        return $this->fetch($sql);
    }
    
    public function getTotalByUid($uid)
    {
        if(!$this->init_db($uid, self::DB_SLAVE ))
            return false;
        
        $where = 'uid='.$uid.' and status=1';
        return $this->fetch_count($where);
    }
    
    public function del_by_id($uid , $id)
    {
        if(!$this->init_db($uid, self::DB_MASTER))
            return false;
        
        $aUpdate = array('status' => 9);
        $this->update($aUpdate , 'atme_id='.$id.' and uid='.$uid , 1);
    }
}
?>